package com.si.ui;

import java.awt.Component;
import java.util.Map;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

public class UI extends JFrame implements IWidget, Runnable {

	private static final long serialVersionUID = 5796567841374948410L;

	private ControlPanel control;
	private PreviewPanel preview;

	@Override
	public void run() {
		this.setVisible(true);
	}

	@Override
	public void initialize(final Map<String, Component> components) {
		this.setTitle("Sentence Imagery");
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setLocationByPlatform(true);
		this.setResizable(false);
		this.buildComponents(components);
		this.buildLayout();
	}

	private void buildComponents(final Map<String, Component> components) {
		final UIMenuBar menuBar = new UIMenuBar();
		menuBar.initialize(components);
		this.setJMenuBar(menuBar);
		this.control = new ControlPanel();
		this.control.initialize(components);
		this.preview = new PreviewPanel();
		this.preview.initialize(components);
	}

	private void buildLayout() {
		final GroupLayout layout = new GroupLayout(getContentPane());
		this.getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(6, 6, 6)
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(this.preview, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(this.control, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addComponent(this.control, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(this.preview, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addContainerGap())
		);
		this.pack();
	}
}
